#define _CRT_SECURE_NO_WARNINGS
#include"test.h"
void SLTPrint(SLTNode* phead)
{
	SLTNode* arr = phead;
	while (arr)
	{
		printf("%d->", arr->data);
		arr = arr->next;
	}
	printf("NULL\n");
}
SLTNode* SLTBuyNode(SLTDataType x)
{
	SLTNode* arr = (SLTNode*)malloc(sizeof(SLTNode));
	if (arr == NULL)
	{
		perror("malloc arr");
		return NULL;
	}
	arr->next = NULL;
	arr->data = x;
	return arr;
}
void SLTPushBack(SLTNode** pphead, SLTDataType x)
{
	assert(pphead);
	SLTNode* arr = SLTBuyNode(x);
	if (*pphead == NULL)
	{
		*pphead = arr;
	}
	else
	{
		SLTNode* ass = *pphead;
		while (ass->next)
		{
			ass = ass->next;
		}
		ass->next = arr;
	}
}
void SLTPushFront(SLTNode** pphead, SLTDataType x)
{
	assert(pphead);
	SLTNode* arr = SLTBuyNode(x);
	arr->next = *pphead;
	*pphead = arr;
}
void SLTPopBack(SLTNode** pphead)
{
	assert(pphead&&*pphead);
	if ((*pphead)->next==NULL)
	{
		free(*pphead);
		*pphead = NULL;
	}
	else
	{
		SLTNode* arr = *pphead;
		SLTNode* ass = *pphead;
		while (arr->next)
		{
			ass = arr;
			arr = arr->next;
		}
		free(arr);
		arr = NULL;
		ass->next = NULL;
	}
	
}
void SLTPopFront(SLTNode** pphead)
{
	assert(pphead && *pphead);
	SLTNode* arr = (*pphead)->next;
	free(*pphead);
	*pphead = arr;
}
SLTNode* SLTFind(SLTNode* phead, SLTDataType x)
{
	SLTNode* arr = phead;
	while (arr)
	{
		if (arr->data == x)
		{
			return arr;
		}
		arr = arr->next;
	}
	return NULL;
}
void SLTInsert(SLTNode** pphead, SLTNode* pos, SLTDataType x)
{
	assert(pphead&&*pphead);
	SLTNode* ass = SLTBuyNode(x);
	if (*pphead == pos)
	{
		ass->next = pos;
		pos = ass;
	}
	else
	{
		SLTNode* arr = *pphead;
		while (arr->next != pos)
		{
			arr = arr->next;
		}
		ass->next = pos;
		arr->next = ass;
	}	
}
void SLTInsertAfter(SLTNode* pos, SLTDataType x)
{
	assert(pos);
	SLTNode* ass = SLTBuyNode(x);
	ass->next = pos->next;
	pos->next = ass;
}

void SLTErase(SLTNode** pphead, SLTNode* pos)
{
	assert(pphead && *pphead);
	assert(pos);
	if (*pphead == pos)
	{
		SLTNode* arr = (*pphead)->next;
		free(*pphead);
		*pphead = arr;
	}
	else
	{
		SLTNode* arr = *pphead;
		while (arr->next != pos)
		{
			arr = arr->next;
		}
		arr->next = pos->next;
		free(pos);
		pos = NULL;
	}
}
void SLTEraseAfter(SLTNode* pos)
{
	assert(pos&&pos->next);	
	SLTNode* arr = pos->next;
	pos->next = arr->next;
	free(arr);
	arr = NULL;
}
void SListDesTroy(SLTNode** pphead)
{
	assert(pphead && *pphead);
	SLTNode* arr = *pphead;
	while (arr)
	{
		SLTNode* ass = arr->next;
		free(arr);
		arr = ass;
	}
	*pphead = NULL;
}